namespace SiliverlightJavascriptIntelli
{
    public static class JavascriptFormats
    {
        public static string JavascriptEventFormat
        {
            get
            {
                return
@"{0}.prototype.add_{1} = function(handler) {
    /// <summary>{2}</summary>
    /// <param name=""handler"" type=""Function"" />
    /// <returns type=""Number"">eventId</returns>
    TypeSafety({""Function"" : handler});
    return this.element.addEventListener(""{1}"", handler);
}
    
{0}.prototype.remove_{1} = function(eventId) {
    /// <summary>{2}</summary>
    /// <param name=""eventId"" type=""Number"">eventId returned from add_{0}</param>
    TypeSafety({""Number"" : eventId});
    this.element.removeEventListener(""{1}"", eventId);
}" + BreakLine;
            }
        }

        public static string BreakLine
        {
            get
            {
                return @"

";
            }
        }

        public static string JavascriptGetter
        {
            get
            {
                return 
@"{0}.prototype.get_{1} = function()
{
    /// <summary>{5}</summary>
    /// <returns type=""{2}"" integer=""{3}"" />   
    return {4}(this.element.{1});
}" + JavascriptFormats.BreakLine;
            }
        }

        public static string JavascriptSetter
        {
            get
            {
                return 
@"{0}.prototype.set_{1} = function(value)
{
    /// <summary>{5}</summary>
    /// <param name=""value"" type=""{2}"" integer=""{3}"" />
    TypeSafety({""{2}"" : value});   
    this.element.{1} = value{4};
}" + JavascriptFormats.BreakLine;
            }
        }



//        public static string JavascriptGetter
//        {
//            get
//            {
//                return @"    get_{0}: function()
//    {
//        /// <value type=""{1}"" integer=""{2}"">{4}</value>   
//        return {3}(this.element.{0});
//    }," + JavascriptFormats.BreakLine;
//            }
//        }

//        public static string JavascriptSetter
//        {
//            get
//            {
//                return @"    set_{0}: function(value)
//    {
//        this.element.{0} = value{3};
//    }," + JavascriptFormats.BreakLine;
//            }
//        }

        public static string JavascriptIndexerGetter
        {
            get
            {
                return
@"{0}.prototype.getItem = function(Index)
{
    /// <summary>Indexer</summary>
    /// <param name=""Index"" type=""Number"" integer=""true"" />
    /// <returns type=""{2}"" integer=""{3}"" />   
    return {4}(this.element.getItem(Index));
}" + JavascriptFormats.BreakLine;
            }
        }

//        public static string JavascriptIndexerSetter
//        {
//            get
//            {
//                return 
//@"{0}.prototype.set_{1}: function(Index, value)
//{
//    /// <param name=""Index"" type=""Number"" integer=""true"" />
//    /// <param name=""value"" type=""{2}"" integer=""{3}"" />  
//    TypeSafety({ ""Number"" : Index, ""{2}"" : value}); 
//    this.element.SetItem(Index, value);
//}" + JavascriptFormats.BreakLine;
//            }
//        }

        public static string JavascriptParamCommentFormat
        {
            get
            {
                return @"    /// <param name=""{0}"" type=""{1}"" integer=""{2}"" >{3}</param>   " + OneBreakLine;

            }
        }

        public static string FunctionEnd
        {
            get
            {
                return @"}" + JavascriptFormats.BreakLine;
            }
        }

        public static string FunctionHeader
        {
            get
            {
                return 
@"{0}.prototype.{1} = function({2})
{
     /// <summary>{3}</summary>
";
            }
        }

        public static string JavascriptReturnsCommentFormat
        {
            get
            {
                return @"    /// <returns type=""{0}"" integer=""{1}"" />   " + OneBreakLine;
            }
        }

        public static string JavacriptFunctionInvocationFormat
        {
            get
            {
                return @"    TypeSafety({4});
    return {2}(this.element.{0}({3}));";
            }
        }

        public static string OneBreakLine
        {
            get
            {
                return @"
";
            }
        }

        public static string Constructor
        {
            get
            {
                return @"{0} = function({0}XamlElement) 
{
        /// <summary>{1}</summary>
        /// <param name=""{0}XamlElement"">Non strongly typed Xaml Element</param>
        this.element = {0}XamlElement;
}" + OneBreakLine;
            }
        }

        public static string ClassEnd
        {
            get
            {
                return string.Empty + BreakLine;
            }
        }

        public static string InitlizeFromhost
        {
            get
            {
                return 
@"{0}.createFromXaml = function(Host)
{
    /// <returns type=""{0}"" />
    if (!Host)
        return;
    try { 
    if (Host.content)
        return new {0}(Host.content.createFromXaml(""<{0} />""));
    } catch (ex) {}
    if (Host.getHost)
        return {0}.CreateNew(Host.getHost());
    return;
}" + OneBreakLine;
            }
        }

        public static string ClassPrototypeForInehrit
        {
            get
            {
                return @"{0}.prototype = new {1};" + BreakLine;
            }
        }

        public static string ConvertToPrefix
        {
            get
            {
                return "Convert.To{0}";
            }
        }



        public static string ConverterTypeJavascriptClassEndDeclaration
        {
            get
            {
                return string.Empty;
            }
        }

        public static string ConverterFunctions
        {
            get
            {
                return @"Convert.Is{0} = function(javascriptObject)
{
    /// <param name=""javascriptObject"">object to check if is {0}</param>
    return CheckConvertIs(""{1}"", javascriptObject, {2});
}

Convert.To{0} = function(javascriptObject)
{
    /// <param name=""javascriptObject"">object to convert to {0}</param>
    /// <returns type=""{0}"" />
    if (!Convert.Is{0}(javascriptObject))
        return;
    else
    {
        if (HasElementProperty(javascriptObject))
            return new {0}(javascriptObject.element)
        else
            return new {0}(javascriptObject);
    }
}
";
            }
        }

//        public static string ConverterFunctions
//        {
//            get
//            {
//                return @"    To{0} : function(xamlObject)
//    {
//                /// <param name=""xamlObject"">object to convert to {0}</param>
//                /// <returns type=""{0}"" />
//                if (!xamlObject)
//                    return;
//                    try {  return new {0}(xamlObject.element); }
//                    catch(ex) { return new {0}(xamlObject); }
//    },
//";
//            }
//        }

        public static string ConverterTypeJavascriptClassDeclaration
        {
            get
            {
                return
                    @"try
{
    if (Convert) 
	    Convert = {};
}
catch(ex)
{
    Convert = {};
}
	";
            }
        }

        public static string ExtractFromWrapper
        {
            get
            {
                return ".element";
            }
        }


        public static string IsPrimitive
        {
            get
            {
                return " || IsPrimitive(javascriptObject)";
            }
        }

        public static string StaticEnumValue
        {
            get
            {
                return
                    @"{1}.get_{0} = function()
{
    /// <returns type=""{1}"" />
    return Convert.To{1}(""{0}"");
}

{1}.is_{0} = function(valueToCompare)
{
    /// <returns type=""bool"" />
    /// <param name=""valueToCompare"" type=""{1}"" />
    return valueToCompare.element == ""{0}"";
}" + BreakLine;
            }
        }

                public static string StaticDependencyProperty
        {
            get
            {
                return
                    @"{1}.get_{0}Property = function()
{
    /// <returns type=""{2}"" />
    return Convert.To{2}(""{1}.{0}"");
}" + BreakLine;
            }
        }

        public static string GetHostForDependencyObject
        {
            get
            {
                return
@"{0}.prototype.getHost = function()
{
    /// <returns type=""Host"" />
    return new Host(this.element.getHost());
}" + BreakLine;
            }
        }

        public static string GetElementForTopOfHierachy
        {
            get
            {
                return
@"{0}.prototype.get_innerElement = function()
{
    /// <returns>Non intellisensed XAML object</returns>
    return this.element; 
}" + BreakLine;
            }
        }
       
    }
}